package com.wims.client;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;

public class ThreeColumnPage extends PageWrapper {

	/** HTML element corresponding to this page */
	public static final String pageName = "workingScreen";

	/** User name label in the greeting */
	private Label userGreeting;

	/** Tree of asset types in the first column */
	private Tree assetTypeTree;

	/** Reference to the login page. Obtained from the holder on first use */
	private LoginPage loginPage = null;

	public ThreeColumnPage(PageHolder holder) {
		super(holder);
	}

	@Override
	public String getPageName() {
		return pageName;
	}

	@Override
	protected void createUI() {
		userGreeting = new Label();
		RootPanel.get("userNameGreeting").add(userGreeting);

		final Button logoutButton = new Button("Logout");
		RootPanel.get("logoutButton").add(logoutButton);
		logoutButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (loginPage == null)
					loginPage = (LoginPage) holder.findPage(LoginPage.pageName);
				// assert (nextPage != null) : "Broken HTML, will die on NPE";
				loginPage.doLogout();
			}
		});

		// The tree in the first column
		assetTypeTree = new Tree();
		RootPanel.get("firstColumn").add(assetTypeTree);
	}

	/** Called by login page on successful login */
	public void doLoginDone(String userName, ArrayList<String> serverReturned) {
		// Populate fields
		userGreeting.setText("Dear " + userName
				+ ", welcome to the ugly prototype of WIMS!");
		// for now initialize with person types or whatever, single depth
		for (String pt : serverReturned) {
			assetTypeTree.addItem(pt);
		}

		// Make this page visible
		holder.setActivePage(pageName);
	}
}
